import Vue from "vue"
import Vuex from "vuex"
import { getToken, removeToken, getCookies, removeCookies } from "../utils/auth";

Vue.use(Vuex)

const store = new Vuex.Store({
  state: {
    adminName: '',
    loginTime: '',
    avatar: '',
    adminDepartment: '',
    adminType: 0,
    testCount: 0,//测试计数器
    token: getToken(),
    setting: {
      //user settings
      //权限菜单显示
    },
    authorization: '',
    assetsAuthorization: '',
    activePath: '',
    treeData: '',
    userTreeData:'',
    user:'',
  },
  //getter类似过滤器加工对数据加工
  getters: {

    adminName: state => state.adminName,
    avatar: state => state.avatar,
    adminDepartment: state => state.adminDepartment,
    adminType: state => state.adminType,
    token: state => state.token,
    loginTime: state => state.loginTime,
    activePath: state => state.activePath,
    authorization: state => state.authorization,
    assetsAuthorization: state => state.assetsAuthorization,
    treeData: state => state.treeData,
    userTreeData: state => state.userTreeData,
    user:state=> JSON.parse(state.user),
  },
  //调用方法改变仓库的值 同步修改 $store.commit("add",n)
  mutations: {
    add(state, n) {
      state.testCount += n
    },
    SET_TOKEN(state, token) {
      state.token = token
    },
    SET_LOGINTIME(state, loginTime) {
      state.loginTime = loginTime
    },
    SET_ADMINNAME(state, adminName) {
      state.adminName = adminName
    },
    SET_AVATAR(state, avatar) {
      state.avatar = avatar
    },
    SET_ADMINTYPE(state, adminType) {
      state.adminType = adminType
    },
    SET_ADMINFDPARTMENT(state, adminDepartment) {
      state.adminDepartment = adminDepartment
    },
    SET_ACTIVEPATH(state, activePath) {
      state.activePath = activePath
    },
    SET_AUTHORIZATION(state, authorization) {
      state.authorization = authorization
    },
    SET_ASSETSAUTHORIZATION(state, assetsAuthorization) {
      state.assetsAuthorization = assetsAuthorization
    },
    SET_TREEDATA(state, treeData) {
      state.treeData = treeData
    },
    SET_USERTREEDATA(state, userTreeData) {
      state.userTreeData = userTreeData
    },
    SET_USER(state, user) {
      state.user = user
    },
  },
  //异步修改值
  actions: {
    //context 表示 mutations方法集
    addAction(context, n) {
      context.commit('add', n)
    },
    //登录储存信息
    loginSaveUserInfo({ commit }) {
      commit("SET_ADMINNAME", getCookies('adminName'));
      commit("SET_AVATAR", getCookies('avatar'));
      commit("SET_ADMINTYPE", getCookies('adminType'));
      commit("SET_ADMINFDPARTMENT", getCookies('department'));
      commit("SET_LOGINTIME", getCookies('loginTime'));
      commit("SET_TOKEN", getCookies('login-key'));
      commit("SET_USER", getCookies('user'));
      commit("SET_AUTHORIZATION", JSON.parse(localStorage.getItem('authorization')));
      commit("SET_ASSETSAUTHORIZATION", JSON.parse(localStorage.getItem('assetsAuthorization')));
    },
    loginOut(context) {
      return new Promise((resolve, reject)=>{
        context.commit('SET_TOKEN', '');
        removeToken();
        removeCookies("adminName");
        removeCookies("avatar");
        removeCookies("adminType");
        removeCookies("department");
        removeCookies("loginTime");
  
        localStorage.removeItem('authorization');
        localStorage.removeItem('assetsAuthorization');

        resolve("MSG:User Already logged out!");
      }).catch(error =>{
        reject(error)
      })

    },
    setActivePath({ commit }, activePath) {
      if (activePath !== undefined) {
        commit("SET_ACTIVEPATH", activePath)
      }
    },
    setTreeData({ commit }, treeData) {
      commit("SET_TREEDATA", treeData)
    }
  },
})
export default store